package com.contain.auth.config;

import com.contain.auth.service.UserDetailsEntity;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: tianchao
 * @Date: 2021/7/6 12:19
 * @Description: 附加 info信息  跟jwt没有关系 仅仅给 OAuth2AccessToken accessToken 添加了一些附加信息
 */

public class InformationTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        UserDetailsEntity userDetailsEntity = (UserDetailsEntity) authentication.getPrincipal();
        String id = userDetailsEntity.getId();
        Map<String, Object> info = new HashMap<>();
        //TODO 到底需要啥
        info.put("name",id);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
        return accessToken;
    }
}
